home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / DELPHI32 / GRAPHICS / TS32 / KEYBOARD.PAS < prev    next >
Pascal/Delphi Source File  |  1995-12-16  |  1KB  |  77 lines

  1. unit KeyboardHandler;
  2.  
  3. (*********************************************
  4. TKeyboardHandler->TComponent
  5.  
  6. Provides on demand state of any key on the
  7. keyboard.
  8.  
  9. Properties:
  10.  
  11. KeyDown-
  12.   A boolean array property.  Return true if the
  13.   specified key is pressed.
  14. *********************************************)
  15. interface
  16.  
  17. uses
  18.   Windows, SysUtils, Messages, Classes, Graphics, Controls,
  19.   Forms, Dialogs;
  20.  
  21. type
  22.   TKeyboardHandler = class( TComponent )
  23.   private
  24.   protected
  25.      function GetKeyDown( c: char ): boolean;
  26.      function GetVirtKey( n: integer ): boolean;
  27.   public
  28.      property KeyDown[c: char]: boolean read GetKeyDown; default;
  29.      property VirtualKeyDown[n: integer]: boolean read GetVirtKey;
  30.   published
  31.   end;
  32.  
  33. procedure Register;
  34.  
  35. implementation
  36.  
  37. function TKeyboardHandler.GetKeyDown( c: char ): boolean;
  38. var
  39.   vk: integer;
  40.   nRC: SHORT;
  41. begin
  42.   vk := 0;
  43.   case c of
  44.      'a'..'z':
  45.         vk := Ord( c ) - Ord( 'a' ) + $41;
  46.      'A'..'Z':
  47.         vk := Ord( c ) - Ord( 'A' ) + $41;
  48.      ' ':
  49.         vk := $20;
  50.   end;
  51. try
  52.   nRC := GetAsyncKeyState( vk );
  53.   Result := nRC < 0;
  54. except
  55.   Result := FALSE;
  56. end;
  57. end;
  58.  
  59. function TKeyboardHandler.GetVirtKey( n: integer ): boolean;
  60. var
  61.   nRC: SHORT;
  62. begin
  63. try
  64.   nRC := GetAsyncKeyState( n );
  65.   Result := nRC < 0;
  66. except
  67.   Result := FALSE;
  68. end;
  69. end;
  70.  
  71. procedure Register;
  72. begin
  73.   RegisterComponents( 'NonVis', [TKeyboardHandler] );
  74. end;
  75.  
  76. end.
  77.